local module = {}
module.name = "Nameplates"
module.Init = function()
	if not SCDB.modules[module.name] then return end
	if SCDB[module.name] == nil then SCDB[module.name] = {} end
	if SCDB[module.name]["AutoEnable"] == nil then SCDB[module.name]["AutoEnable"] = true end
	if SCDB[module.name]["AutoOff"] == nil then SCDB[module.name]["AutoOff"] = false end
	if SCDB[module.name]["Friendly Names"] == nil then SCDB[module.name]["Friendly Names"] = true end
	local opts = SCDB[module.name]

	local nameplates = CreateFrame("Frame", nil, UIParent)
	nameplates:SetScript("OnEvent", function(self, event, ...) self[event](self, ...) end)
	
	SetCVar("ShowClassColorInNameplate", 1)

	rgb2str = function(col) local r, g, b = unpack(col) return format("|cff%02x%02x%02x", r*255, g*255, b*255) end
	rgb2str = function(r, g, b) return format("|cff%02x%02x%02x", r*255, g*255, b*255) end

	if SCDB[module.name]["AutoEnable"] == true then
		if SCDB[module.name]["Friendly Names"] then
			SetCVar("nameplateShowFriends", 1)
		end
		SetCVar("nameplateShowEnemies", 1)
		SetCVar("UnitNameEnemyTotemName", 1)
		SetCVar("nameplateShowEnemyTotems", 1)
	end
	
	local styleNamePlate = function(self)
		local healthBar, castBar = self:GetChildren()
		if not healthBar and not castBar then return end
		if self.styled == true then return end

		local barfill, castborderTexture, shield, castbaricon = castBar:GetRegions()
		local threatTexture, borderTexture, highlightTexture, nameText, levelText, bossIcon, raidIcon, dragonTexture = self:GetRegions() 
		
		local r, g, b, a
		
		healthBar:SetHeight(10)
		
		local r,g,b = healthBar:GetStatusBarColor()
		
		healthBar.bg = healthBar:CreateTexture(nil, "BACKGROUND")
		healthBar.bg:SetAllPoints(healthBar)
		healthBar.bg:SetTexture(0,0,0)
		
		healthBar.hpGlow = CreateFrame("Frame", nil, healthBar)
		healthBar.hpGlow:SetPoint("TOPLEFT", healthBar, "TOPLEFT", -4.5, 4.5)
		healthBar.hpGlow:SetPoint("BOTTOMRIGHT", healthBar, "BOTTOMRIGHT", 4.5, -4.5)
		healthBar.hpGlow:SetBackdrop({edgeFile = "Interface\\Addons\\"..addonName.."\\media\\texture\\glowtex", edgeSize = 5, insets = {left = 3, right = 3, top = 3, bottom = 3}})
		healthBar.hpGlow:SetBackdropColor(0, 0, 0 , .5)
		healthBar.hpGlow:SetBackdropBorderColor(0, 0, 0, .5)
		healthBar.hpGlow:SetFrameStrata("LOW")
		
		local r,g,b = healthBar:GetStatusBarColor()
		r,g,b = floor(r*100+.5)/100, floor(g*100+.5)/100, floor(b*100+.5)/100
		
		self.name = self:CreateFontString(nil, "BORDER")
		self.name:SetFont(STANDARD_TEXT_FONT, 10, "THINOUTLINE")
				
		self.friendly = true
		if (r == 0 and g == 0 and b == 1) then
			self.name:SetTextColor(0,.7,1)
		elseif (r==0 and g==1 and b==0) then
			self.name:SetTextColor(0,1,.7)
		elseif (r==1 and g==1 and b==0) then
			self.name:SetTextColor(.7,1,0)
		else
			self.name:SetTextColor(r,g,b)
		end
		
		if r==1 and b==0 and g==0 then
			self.friendly = false
		end
		
		if SCDB[module.name]["Friendly Names"] then
			if self.friendly == true then
				healthBar:Hide()
				castBar:Hide()
			else
				healthBar:Show()
				castBar:Show()
			end
		end

		castBar:SetStatusBarTexture(self.friendly==false and "Interface\\Addons\\"..addonName.."\\media\\texture\\statusbar" or nil)
		
		highlightTexture:SetTexture(self.friendly==false and "Interface\\Addons\\"..addonName.."\\media\\texture\\statusbar" or nil)
		highlightTexture:ClearAllPoints()
		highlightTexture:SetAllPoints(healthBar)
		highlightTexture:SetAlpha(.5)

		healthBar:SetStatusBarTexture("Interface\\Addons\\"..addonName.."\\media\\texture\\statusbar", "BACKGROUND")
		
		self.name:SetJustifyH("CENTER")
		self.name:SetPoint("BOTTOM", healthBar, "TOP", 0, 2)

		local r,g,b = levelText:GetTextColor()
		self.name:SetText(rgb2str(r,g,b)..((levelText:GetText() or "")..(dragonTexture:IsShown() == 1 and "+" or "")).."|r "..nameText:GetText() or "")

		--boss icon
		bossIcon:ClearAllPoints()
		bossIcon:SetPoint("RIGHT", self.name, "LEFT", 0, 0)

		--castbars
		local w,h = healthBar:GetSize()
		castBar:SetSize(w,h)
		castBar:ClearAllPoints()
		castBar:SetPoint("BOTTOM",healthBar,0,-5)

		castbaricon:ClearAllPoints()
		castbaricon:SetSize(h,h)
		castbaricon:SetPoint("TOP", healthBar, "BOTTOM", 0, -5)
		castbaricon:SetTexCoord(0.1,0.9,0.1,0.9)
		
		shield:ClearAllPoints()
		shield:SetTexture("Interface\\CastingBar\\UI-CastingBar-Small-Shield")
		shield:SetTexCoord(0, 36/256, 0, 1)
		shield:SetSize(16,44)
		shield:SetPoint("CENTER", castbaricon, 0, 0)
		
		--castbar bg
		castBar.bg = CreateFrame("Frame", nil, castBar)
		castBar.bg:SetPoint("TOPLEFT", castbaricon, -1, 1)
		castBar.bg:SetPoint("BOTTOMRIGHT", castBar, 1, -1)
		castBar.bg:SetBackdrop({ edgeFile = "Interface\\BUTTONS\\WHITE8X8", bgFile = "Interface\\BUTTONS\\WHITE8X8", edgeSize = 1, tileSize = 1, insets = {left=0, right=0, top=0, bottom=0}, })
		castBar.bg:SetBackdropBorderColor(0,0,0,1)
		castBar.bg:SetBackdropColor(0,0,0,.5)
		castBar.bg:SetFrameLevel(castBar:GetFrameLevel()-1)

		castBar.shield = shield
		castBar.icon = castbaricon
		
		castBar:HookScript("OnShow", function(self)
			local w,h = healthBar:GetSize()
			self:SetSize(w,h)
			self:ClearAllPoints()
			self:SetPoint("TOP", healthBar, "BOTTOM", 0, -5)

			self.icon:ClearAllPoints()
			self.icon:SetSize(h,h)
			self.icon:SetPoint("LEFT", self, 0, 0)
			self.icon:SetTexCoord(.1, .9, .1, .9)

			self.bg:SetPoint("TOPLEFT", self.icon, -1, 1)
			self.bg:SetPoint("BOTTOMRIGHT", castBar, 1, -1)
			
			self.shield:ClearAllPoints()
			self.shield:SetTexture("Interface\\CastingBar\\UI-CastingBar-Small-Shield")
			self.shield:SetTexCoord(0, 36/256, 0, 1)
			self.shield:SetSize(16,44)
			self.shield:SetPoint("CENTER", self.icon, 0, 0)
		end)

		--raidicon
		raidIcon:ClearAllPoints()
		raidIcon:SetSize(20,20)
		raidIcon:SetPoint("BOTTOM",healthBar,"TOP",0,17)

		--disable some stuff
		nameText:Hide()
		levelText:Hide()
		dragonTexture:SetTexture(nil) --just plain ugly!
		borderTexture:SetTexture(nil)
		castborderTexture:SetTexture(nil)

		threatTexture:SetTexture(nil)
		healthBar.threat = self:CreateTexture(nil, "OVERLAY")
		healthBar.threat:SetTexture(1,0,0,.4)
		healthBar.threat:SetBlendMode("ADD")
		healthBar.threat:SetAllPoints(healthBar:GetStatusBarTexture())
		healthBar.threat:Hide()
		
		self:HookScript("OnUpdate", function(s, elapsed)
			if not s.sa then s.sa = 0 end
			s.sa = s.sa + elapsed
			
			if s.sa > .1 then
				local healthBar, castBar = s:GetChildren()
				local castborderTexture, barfill, shield, castbaricon = castBar:GetRegions()
				local threatTexture, borderTexture, highlightTexture, nameText, levelText, bossIcon, raidIcon, dragonTexture = s:GetRegions() 
				
				if threatTexture:IsVisible() then
					healthBar.threat:Show()
					self.friendly = false
				else
					healthBar.threat:Hide()
				end
				
				s.name:ClearAllPoints()
				if s.name:GetStringWidth() > healthBar:GetWidth() then
					s.name:SetSize(unpack({healthBar:GetSize()}))
					s.name:SetPoint("BOTTOM", healthBar, "TOP", 0, 2)
				else
					s.name:SetPoint("BOTTOM", healthBar, "TOP", 0, 2)
				end
				
				s.sa = 0
			end
		end)

		
		self:HookScript("OnShow", function(self) 
			local healthBar, castBar = self:GetChildren()
			local castborderTexture, barfill, shield, castbaricon = castBar:GetRegions()
			local threatTexture, borderTexture, highlightTexture, nameText, levelText, bossIcon, raidIcon, dragonTexture = self:GetRegions() 
			
			healthBar:SetHeight(10)
			
			local r,g,b = healthBar:GetStatusBarColor()

			local r,g,b = healthBar:GetStatusBarColor()
			r,g,b = floor(r*100+.5)/100, floor(g*100+.5)/100, floor(b*100+.5)/100

			if (r == 0 and g == 0 and b == 1) then
				self.name:SetTextColor(0,.7,1)
			elseif (r==0 and g==1 and b==0) then
				self.name:SetTextColor(0,1,.7)
			elseif (r==1 and g==1 and b==0) then
				self.name:SetTextColor(.7,1,0)
			else
				self.name:SetTextColor(r,g,b)
			end
			
			self.friendly = true
			if r==1 and b==0 and g==0 then
				self.friendly = false
			end
			
			if SCDB[module.name]["Friendly Names"] then
				if self.friendly == true then
					healthBar:Hide()
					castBar:Hide()
				else
					healthBar:Show()
					castBar:Show()
				end
			end

			self.name:SetText(rgb2str(levelText:GetTextColor())..((levelText:GetText() or "")..(dragonTexture:IsShown() == 1 and "+" or "")).."|r "..nameText:GetText() or "")
			
			highlightTexture:SetTexture(self.friendly==false and "Interface\\Addons\\"..addonName.."\\media\\texture\\statusbar" or nil)
			highlightTexture:ClearAllPoints()
			highlightTexture:SetAllPoints(healthBar)
			highlightTexture:SetAlpha(.5)
			
			healthBar:SetStatusBarTexture("Interface\\Addons\\"..addonName.."\\media\\texture\\statusbar", "BACKGROUND")
			levelText:Hide()
			nameText:Hide()
		end)

		self.styled = true
	end

	local IsNamePlateFrame = function(self)
		if self:GetName() and self:GetName():find("NamePlate") then
			return true 
		else
			return false
		end
	end
  
	nameplates:SetScript("OnUpdate", function(self, elapsed)
		if not self.lastupdate then self.lastupdate = 0 end
		self.lastupdate = self.lastupdate + elapsed
		if self.lastupdate > 0.1 then
			self.lastupdate = 0
			local num = select("#", WorldFrame:GetChildren())
			for i = 1, num do
				local f = select(i, WorldFrame:GetChildren())
				if not self.styled and IsNamePlateFrame(f) then 
					styleNamePlate(f)
				end
			end
		end
	end)
	
	
	nameplates:RegisterEvent("PLAYER_REGEN_ENABLED")
	nameplates:RegisterEvent("PLAYER_REGEN_DISABLED")

	nameplates.PLAYER_REGEN_ENABLED = function()
		if SCDB[module.name]["AutoOff"] == true then
			if SCDB[module.name]["AutoEnable"] == true then
				if SCDB[module.name]["Friendly Names"] then
					SetCVar("nameplateShowFriends", 1)
				end
				
				SetCVar("nameplateShowEnemies", 0)
				SetCVar("UnitNameEnemyTotemName", 0)
				SetCVar("nameplateShowEnemyTotems", 0)
			end
		end
	end
	
	nameplates.PLAYER_REGEN_DISABLED = function()
		if SCDB[module.name]["AutoEnable"] == true then
			if SCDB[module.name]["Friendly Names"] then
				SetCVar("nameplateShowFriends", 1)
			end
			SetCVar("nameplateShowEnemies", 1)
			SetCVar("ShowClassColorInNameplate", 1)
			SetCVar("UnitNameEnemyTotemName", 1)
			SetCVar("nameplateShowEnemyTotems", 1)
		end
	end
	
end
tinsert(SuperClassic.modules, module) -- finish him!